package primary.primary0;

import java.util.ArrayList;
import java.util.List;

public class S0051N皇后 {
    public static class Solution {
        /**
         * 32， 改了好几次，主要遗漏点在return的位置，回溯确实会经常漏return
         * 因为一直在用同一个record，所以记住 mark:如果写了record，那么对record的复原一定不能漏
         * 我就是判断下一个节点为null的时候，return了，才导致record里面有脏数据，不debug很难发现问题
         * 因为需要所有的case，因此用回溯
         * 用四个array来记录吧
         */
        public List<List<String>> solveNQueens(int n) {
            if(n == 0){
                return new ArrayList<>();
            }
            boolean[] record1 = new boolean[n];
            boolean[] record2 = new boolean[n];
            boolean[] record3 = new boolean[2*n - 1];
            boolean[] record4 = new boolean[2*n - 1];

            nQueenHelp(n, n, 0, 0, record1, record2, record3, record4, new ArrayList<>());

            return res;
        }

        private List<List<String>> res = new ArrayList<>();

        private void nQueenHelp(int numsLeft, int n, int i, int j, boolean[] record1, boolean[] record2, boolean[] record3, boolean[] record4, List<int[]> curPath){
            if(numsLeft == 0 || record1[i] || record2[j] || record3[i + j] || record4[n - 1 + i - j]){
                // 找下一个遍历的点
                int[] nextPosi = nextPosi(n, i, j);
                if(nextPosi != null){
                    nQueenHelp(numsLeft, n, nextPosi[0], nextPosi[1], record1, record2, record3, record4, curPath);;
                }
                return;
            }
            // 其实这里可以有很多剪枝操作， 注意 || (j >= 1 && !record2[j - 1]) 不能加上去，因为我j可能上一列没有record，这很正常，但是i，上一行一定要出一个值，因为我们是逐行遍历的，和nextPosi的逻辑有关
            if(i >= 1 && !record1[i - 1]){
                return;
            }


            numsLeft = numsLeft - 1;
            record1[i] = true;
            record2[j] = true;
            record3[i + j] = true;
            record4[n - 1 + i - j] = true;
            curPath.add(new int[]{i, j});

            if(numsLeft == 0){
                appendRes(curPath);
            }else{
                int[] nextPosi = nextPosi(n, i, j);
                if(nextPosi != null){
                    // mark: 这里千万不能return，容易导致record脏数据
                    nQueenHelp(numsLeft, n, nextPosi[0], nextPosi[1], record1, record2, record3, record4, curPath);
                }

            }

            numsLeft = numsLeft + 1;
            record1[i] = false;
            record2[j] = false;
            record3[i + j] = false;
            record4[n - 1 + i - j] = false;
            curPath.remove(curPath.size() - 1);

            int[] nextPosi = nextPosi(n, i, j);
            if(nextPosi != null){
                nQueenHelp(numsLeft, n, nextPosi[0], nextPosi[1], record1, record2, record3, record4, curPath);
            }

        }

        // 换成sb快了挺多
        private void appendRes(List<int[]> curPath){
            int totalSize = curPath.size();
            List<String> resEle = new ArrayList<>();
            for (int[] ints : curPath) {
                StringBuilder row = new StringBuilder();
                for (int j = 0; j < totalSize; j++) {
                    if (ints[1] == j) {
                        row.append("Q");
                    } else {
                        row.append(".");
                    }
                }
                resEle.add(row.toString());
            }
            res.add(resEle);
        }

        private int[] nextPosi(int n, int i, int j){
            if(j < n - 1){
                return new int[]{i, j + 1};
            }else{
                if(i < n - 1){
                    return new int[]{i + 1, 0};
                }else{
                    return null;
                }
            }
        }
    }
}
